package q605_canPlaceFlowers;

public class Solution {
    /*
    朴素解法：
    只要发现某个位置的前后都是0，就种一朵花
    注意开始和结束位置的特判情况
    最后比较count >= n
     */
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        int count = 0, p1 = 0;
        if ((flowerbed.length > 1 && flowerbed[0] == 0 && flowerbed[1] == 0) || (flowerbed.length == 1 && flowerbed[0] == 0)) {
            count++;
            flowerbed[0] = 1;
        }
        for (int i = 1; i < flowerbed.length - 1; i++) {
            if (flowerbed[i] == 0 && flowerbed[i - 1] == 0 && flowerbed[i + 1] == 0) {
                count++;
                flowerbed[i] = 1;
            }
        }

        if (flowerbed.length > 1 && flowerbed[flowerbed.length - 1] == 0 && flowerbed[flowerbed.length - 2] == 0) count++;


        return count >= n;
    }
}
